package com.lancoo.cvas.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.cvas.aspect.LogAnnotation;
import com.lancoo.cvas.entity.Student;
import com.lancoo.cvas.pojo.common.PageInfo;
import com.lancoo.cvas.pojo.dto.CacheTeachingClassDto;
import com.lancoo.cvas.pojo.vo.*;
import com.lancoo.cvas.service.CourseService;
import com.lancoo.cvas.service.CssTaskInfoService;
import com.lancoo.cvas.service.StudentService;
import com.lancoo.cvas.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "学生选课")
@RestController
@RequestMapping("/api/v5.0.1/web/student")
public class StudentController {


    @Resource
    private StudentService studentService;

    @Resource
    private CssTaskInfoService cssTaskInfoService;

    @Resource
    private CourseService courseService;

    @ApiOperation("获取全部学生")
    @GetMapping(value = "/getStudentList")
    @LogAnnotation(operationType = "3", operationContent = "获取全部学生")
    public Result<PageInfo<Student>> getStudentByConditions(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "年级id ", required = false) @RequestParam(required = false) String gradeId,
                                                            @ApiParam(value = "班级id ", required = false) @RequestParam(required = false) String classId,
                                                            @ApiParam(value = "名称、学号模糊查询", required = false) @RequestParam(required = false) String keyWord,
                                                            @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                            @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Student> page = new Page<>(currentPage, pageSize);
        return Result.ofSuccess(studentService.getStudentByConditions(taskId, page, gradeId, classId, keyWord));
    }


    @ApiOperation("获取选课任务")
    @PostMapping(value = "/getCssTask")
    @LogAnnotation(operationType = "3", operationContent = "获取选课任务")
    public Result<List<CssTaskRoundInfo>> getCssTask(@ApiParam(value = "获取选课计划入参实体类", required = true) @RequestBody GetCssTask getCssTask) {
        return Result.ofSuccess(cssTaskInfoService.getCssTaskBySchool(getCssTask));
    }


    @ApiOperation("根据基础平台学生Id获取学生信息")
    @GetMapping(value = "/getStudentInfoByBaseStudentId")
    @LogAnnotation(operationType = "3", operationContent = "根据基础平台学生Id获取学生信息")
    public Result<Student> getStudentInfoByBaseStudentId(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "基础平台学生id ", required = true) @RequestParam String baseStudentId) {
        return Result.ofSuccess(studentService.getStudentByBaseStudentId(taskId, baseStudentId));
    }


    @ApiOperation("获取课程/环节类别")
    @GetMapping(value = "/getType")
    @LogAnnotation(operationType = "3", operationContent = "获取课程/环节类别")
    public Result<List<CourseTypeDto>> getTeacherPlan(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                      @ApiParam(value = "学生id", required = true) @RequestParam Long studentId,
                                                      @ApiParam(value = "基础平台学生id", required = true) @RequestParam String baseStudentId
    ) {
        return Result.ofSuccess(studentService.getCourseTypeByTaskId(taskId, studentId,baseStudentId));
    }


    @ApiOperation("获取学生选课界面信息")
    @PostMapping(value = "/getStudentTeachingClassView")
    @LogAnnotation(operationType = "3", operationContent = "获取学生选课界面信息")
    public Result<StudentTeachingClassView> getAvailableClass(@ApiParam(value = "学生对应信息", required = true) @RequestBody GetAvailableCondition getAvailableCondition) {
        return Result.ofSuccess(studentService.getAvailableTeachingClass(getAvailableCondition));
    }


    @ApiOperation("更新重修学生状态")
    @GetMapping(value = "/updateIsRetake")
    public Result<String> updateIsRetake(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(studentService.updateIsRetake(taskId));
    }



    @ApiOperation("学生选课")
    @PostMapping(value = "/selectCourseOptimize")
    @LogAnnotation(operationType = "2", operationContent = "学生选课")
    public Result<Object> selectCourseOptimize(@ApiParam(value = "选课实体类", required = true) @RequestBody AddSelectCourse addSelectCourse) {

        int res = studentService.selectCourseOptimize(addSelectCourse);
        switch (res) {
            case 1:
                return Result.ofSuccess("选课成功");
            case -1:
                return Result.ofFail(2, "选课已满，无法继续选课");
            case -2:
                return Result.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -3:
                return Result.ofFail(5, "已添加相同课程下的教学班，请勿重复选择");
            case -4:
                return Result.ofFail(2, "不存在该教学班");
            default:
                return Result.ofFail(2, "系统处理异常，选课失败");
        }
    }


    @ApiOperation("获取无需选课的课程 环节教学班")
    @GetMapping(value = "/getNormalTeachingClass")
    public Result<List<StudentTeachingClass>> getNormalTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                     @ApiParam(value = "学生id", required = true) @RequestParam Long studentId) {
        return Result.ofSuccess(studentService.getNormalTeachingClass(taskId, studentId));
    }


    @ApiOperation("获取班级信息")
    @GetMapping(value = "/getClassDto")
    @LogAnnotation(operationType = "3", operationContent = "获取班级信息")
    public Result<List<BaseClassInfoDto>> getClassDto(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(studentService.getClassDto(taskId));
    }


    @ApiOperation("清空学生选课缓存")
    @DeleteMapping(value = "/cacheStudent")
    public Result<Boolean> syncTeachingClassStudent(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return Result.ofSuccess(studentService.syncTeachingClassStudent(taskId));
    }


    @ApiOperation("清空学生对应教学班缓存")
    @PostMapping(value = "/cacheTeachingClass")
    public Result<Boolean> cacheTeachingClass(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                              @ApiParam(value = "教学班id", required = true) @RequestBody CacheTeachingClassDto cacheTeachingClassDto) {
        return Result.ofSuccess(studentService.clearCacheByTeachingClass(taskId,cacheTeachingClassDto));
    }


    @ApiOperation("退选2")
    @PostMapping(value = "/cancelCourseOptimize")
    @LogAnnotation(operationType = "2", operationContent = "学生退选")
    public Result<String> cancelCourseOptimize(@ApiParam(value = "退选实体类", required = true) @RequestBody AddCancelCourse addCancelCourse) {
        int res = studentService.cancelCourseOptimize(addCancelCourse);
        switch (res) {
            case 1:
                return Result.ofSuccess("退选成功");
            case -2:
                return Result.ofFail(2, "Redis服务连接异常，请联系管理员");
            case -4:
                return Result.ofFail(2, "不存在该教学班");
            default:
                return Result.ofFail(2, "系统处理异常，退选失败");
        }
    }

}
